﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL.Interface;
using Model;

namespace DAL.Dao
{
    public class DeliverymanDaoimpl : Base, IDeliveryDao
    {
        public int ExistNum { get { return DbEntity.expressDBEntities.Deliveryman.Count(); } private set { } }
        public DeliverymanDaoimpl()
        {
            DbEntity.CheckConn();
        }
        public IList<Deliveryman> GetList()
        {
            DbEntity.CheckConn();
            return  (from i in DbEntity.expressDBEntities.Deliveryman orderby i.ID select i).ToList();
        }

        public Deliveryman GetItemByID(int id)
        {
            DbEntity.CheckConn();
            return (from i in DbEntity.expressDBEntities.Deliveryman
                    where i.ID==id
                    select i).ToList().FirstOrDefault();
        }
        public (int,IList<Deliveryman>) GetList(int rows,int page)
        {
            DbEntity.CheckConn();
            var rs = (from i in DbEntity.expressDBEntities.Deliveryman orderby i.ID select i).ToList();
            return (GetTotalPages(rs.Count, rows), rs.Skip((page-1)*rows).Take(rows).ToList());
        }


        public IList<Deliveryman> GetDeliverymanInfo(string username)
        {
            DbEntity.CheckConn();
            //使用Lambda表达式查询数据
            var info = (from i in DbEntity.expressDBEntities.Deliveryman
                        where i.UserName == username
                        select i).ToList();

            return info;
        }


        public bool Delete(int id)
        {
            Deliveryman deliveryman = DbEntity.expressDBEntities.Deliveryman.Find(id);
            if (deliveryman == null) return false;
            DbEntity.expressDBEntities.Deliveryman.Remove(deliveryman);
            DbEntity.expressDBEntities.SaveChanges();
            return true;
        }
        public IList<Deliveryman> GetDeliverymanInfo(int id)
        {
            DbEntity.CheckConn();
            if (DbEntity.expressDBEntities == null) DbEntity.expressDBEntities = new ExpressDBEntities();
            //使用Lambda表达式查询数据
            var info = (from i  in DbEntity.expressDBEntities.Deliveryman
                       where i.ID == id
                       select i).ToList();

            return info;
        }

        public Deliveryman Find(int id)
        {
            DbEntity.CheckConn();
            return DbEntity.expressDBEntities.Deliveryman.Find(id);
        }
        public bool Add(Deliveryman deliveryman)
        {
            DbEntity.CheckConn();
            deliveryman.ID = GetList().Last().ID;
            DbEntity.expressDBEntities.Deliveryman.Add(deliveryman);
            DbEntity.expressDBEntities.SaveChanges();
            return true;
        }



        public Deliveryman Login(string username, string userpass)
        {
            DbEntity.CheckConn();
            return  (from i in DbEntity.expressDBEntities.Deliveryman
                                      where i.UserName.Trim().Equals(username) && i.UserPass.Trim().Equals(userpass)
                                  select i).ToList().FirstOrDefault();
       
        }
      

        public bool Update(int id, Deliveryman newitem)
        {

            DbEntity.CheckConn();
            Deliveryman item = (from i in DbEntity.expressDBEntities.Deliveryman
                       where i.ID==id
                       select i).ToList().FirstOrDefault();
            if (item == null)
            {
                return false;
            }



            //DbEntityEntry<Bag> entry = DbEntity.expressDBEntities.Entry<Bag>(newitem);
            //3.设置该对象为被修改过
            //entry.State = EntityState.Unchanged;
            //4.设置该对象的 uName属性为修改状态，同时 entry.State 被修改为 Modified 状态
            item.UserName = newitem.UserName;
            item.UserPass = newitem.UserPass;
            item.TrueName = newitem.TrueName;
            item.Phone = newitem.Phone;
            item.IDCard = newitem.IDCard;
            item.UpdateTime = newitem.UpdateTime;
            item.AddTime = newitem.AddTime;

      
            DbEntity.expressDBEntities.SaveChanges();

            return true;
        }




        public void Dispose()
        {
            if (DbEntity.expressDBEntities != null) DbEntity.expressDBEntities.Database.Connection.Close();
        }
    }
}
